package com.vrv.netmgr.entry;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.GlobalConstants;
import com.vrv.common.UrlPrefix;
import com.vrv.common.util.HttpClientToServer;
import com.vrv.netmgr.R;
import com.vrv.netmgr.persistent.LoginSharedPreferencesHelper;
import com.vrv.netmgr.vo.LoginInfo;

/**
 * @Description 今日数据提示界面
 *
 * @author gaopei
 * @date
 */
@SuppressLint("HandlerLeak")
public class TodayInfoActivity extends Activity {
    private boolean isBack;
    /**
     * 查询
     */
    private static final int QUERY_IllegalOutConn = 1;
    private static final int QUERY_DeviceChg = 2;
    private static final int QUERY_IPBindChg = 3;
    private static final int QUERY_DetectorUninstall = 4;
    private static final int QUERY_FlowAbnormal = 5;
    private static final int QUERY_PreventAlarm = 6;
    private static final int NET_ERR = 99;

    public String IllegalOutConn_RECORD_COUNT = null;
    public String DeviceChg_RECORD_COUNT = null;
    public String IPBindChg_RECORD_COUNT = null;
    public String DetectorUninstall_RECORD_COUNT = null;
    public String FlowAbnormal_RECORD_COUNT = null;
    public String PreventAlarm_RECORD_COUNT = null;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.today_info_main);
        for (int i = 1; i < 7; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    getDataFromServer(index);
                    handler.sendEmptyMessage(index);
                }
            }).start();
        }
    }

    /**
     * 处理器
     */
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case QUERY_IllegalOutConn:
                TextView textView1 = (TextView) findViewById(R.id.commentWidget_0);
                if (!"0".equals(IllegalOutConn_RECORD_COUNT)) {
                    textView1.setText(IllegalOutConn_RECORD_COUNT);
                    textView1.setVisibility(View.VISIBLE);
                }
                break;
            case QUERY_DeviceChg:
                TextView textView2 = (TextView) findViewById(R.id.commentWidget_1);
                if (!"0".equals(DeviceChg_RECORD_COUNT)) {
                    textView2.setText(DeviceChg_RECORD_COUNT);
                    textView2.setVisibility(View.VISIBLE);
                }
                break;
            case QUERY_IPBindChg:
                TextView textView3 = (TextView) findViewById(R.id.commentWidget_2);
                if (!"0".equals(IPBindChg_RECORD_COUNT)) {
                    textView3.setText(IPBindChg_RECORD_COUNT);
                    textView3.setVisibility(View.VISIBLE);
                }
                break;
            case QUERY_DetectorUninstall:
                TextView textView4 = (TextView) findViewById(R.id.commentWidget_3);
                if (!"0".equals(DetectorUninstall_RECORD_COUNT)) {
                    textView4.setText(DetectorUninstall_RECORD_COUNT);
                    textView4.setVisibility(View.VISIBLE);
                }
                break;
            case QUERY_FlowAbnormal:
                TextView textView5 = (TextView) findViewById(R.id.commentWidget_4);
                if (!"0".equals(FlowAbnormal_RECORD_COUNT)) {
                    textView5.setText(FlowAbnormal_RECORD_COUNT);
                    textView5.setVisibility(View.VISIBLE);
                }
                break;
            case QUERY_PreventAlarm:
                TextView textView6 = (TextView) findViewById(R.id.commentWidget_5);
                if (!"0".equals(PreventAlarm_RECORD_COUNT)) {
                    textView6.setText(PreventAlarm_RECORD_COUNT);
                    textView6.setVisibility(View.VISIBLE);
                }
                break;
            case NET_ERR:
                Toast.makeText(TodayInfoActivity.this, "网络连接失败", Toast.LENGTH_LONG).show();
                break;
            }
        }
    };

    // 查询所有数据
    public void getDataFromServer(int index) {
        Map<String, String> map = new HashMap<String, String>();
        switch (index) {
        case QUERY_IllegalOutConn:
            map.put(GlobalConstants.ERR_TYPE, "0");
            break;
        case QUERY_DeviceChg:
            map.put(GlobalConstants.ERR_TYPE, "1");
            break;
        case QUERY_IPBindChg:
            map.put(GlobalConstants.ERR_TYPE, "2");
            break;
        case QUERY_DetectorUninstall:
            map.put(GlobalConstants.ERR_TYPE, "5");
            break;
        case QUERY_FlowAbnormal:
            map.put(GlobalConstants.ERR_TYPE, "8");
            break;
        case QUERY_PreventAlarm:
            map.put(GlobalConstants.ERR_MSG, "阻断");
            break;
        }
        boolean retVal = qryTodayInfo(map, index);
        if (!retVal)
            handler.sendEmptyMessage(NET_ERR);
    }

    public boolean qryTodayInfo(Map<String, String> map, int index) {
        boolean retVal = false;
        HttpClientToServer httpClientToServer = new HttpClientToServer();
        String urlPrefix = new UrlPrefix(getApplicationContext()).getUrlPrefix();
        String urlAddr = urlPrefix + "/errorMessage/getUnreadErrorInfoCount.do";
        String response = httpClientToServer.doPost(urlAddr, map);

        if (HttpClientToServer.DO_POST_ERR.equals(response) || HttpClientToServer.DO_POST_EXCEPT.equals(response))
            return retVal;

        try {
            JSONTokener jsonParser = new JSONTokener(response);
            JSONObject jsonObj = (JSONObject) jsonParser.nextValue();

            retVal = jsonObj.getBoolean("success");
            if (!retVal)
                return retVal;
            int count = jsonObj.getInt("totalRecords");

            switch (index) {
            case QUERY_IllegalOutConn:
                IllegalOutConn_RECORD_COUNT = count + "";
                break;
            case QUERY_DeviceChg:
                DeviceChg_RECORD_COUNT = count + "";
                break;
            case QUERY_IPBindChg:
                IPBindChg_RECORD_COUNT = count + "";
                break;
            case QUERY_DetectorUninstall:
                DetectorUninstall_RECORD_COUNT = count + "";
                break;
            case QUERY_FlowAbnormal:
                FlowAbnormal_RECORD_COUNT = count + "";
                break;
            case QUERY_PreventAlarm:
                PreventAlarm_RECORD_COUNT = count + "";
                break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return retVal;
    }

    @Override
    protected void onResume() {
        super.onResume();

        for (int i = 1; i < 7; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    getDataFromServer(index);
                    handler.sendEmptyMessage(index);
                }
            }).start();
        }

    }

    /**
     *
     */
    Handler backHandler = new Handler() {
        public void handleMessage(Message msg) {
            isBack = false;
        }
    };

    @Override
    public void onBackPressed() {
    	LoginInfo loginInfo = LoginSharedPreferencesHelper.getLoginInfo(getApplicationContext());
        if (!isBack) {
            isBack = true;
            if(loginInfo.isAutoLogin()){
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getApplicationContext(), "再按一次到登录界面", Toast.LENGTH_SHORT).show();
			}
            backHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
        	if(loginInfo.isAutoLogin()){
				finish();
				System.exit(0);
			}else{
				finish();
				startActivity(new Intent(TodayInfoActivity.this, LoginActivity.class));
			}
        }
    }
}
